$(function () {
    // 默认加载部门树
    vm.getDept();
    $("#jqGrid").Grid({
        url: '../device/stat/faultHandleDurationTopWork',
        colModel: [
            {label: '报修编号', name: 'repairCode'},
            {label: '产线-工序', name: 'devIdCode'},
            {label: '设备名称', name: 'devName'},
            {label: '故障代码', name: 'faultCode'},
            {label: '接单时长', name: 'jiedanDuration'},
            {label: '维修时长', name: 'weixiuDuration'},
            {label: '效果确认时长', name: 'querenDuration'}
        ]
    });
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "name" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    vm.q.deptId = treeObj.deptId;
    vm.search();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            month: '', // 统计月份
            deptId: '01' // 设备组织CODE
        },
        chartTitle: '',
        option: {
            title: {
                text: '工单执行平均时长统计(分钟)',
                left: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: '{b} <br/>{a} : {c} ({d}%)'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                data: []
            },
            series: [
                {
                    name: '平均时长',
                    type: 'pie',
                    radius: '60%',
                    center: ['40%', '50%'],
                    data: [],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                }
            });
        },
        search: function () {
            let self = this;
            var param = {};
            if (this.q.month) {
                param.month = this.convertGMT2Month(this.q.month);
            }
            if (this.q.deptId) {
                param.deptId = this.q.deptId;
            }
            if (this.q.topNum) {
                param.topNum = this.q.topNum;
            }
            myChart.showLoading();
            // 加载设备状态概览
            Ajax.request({
                url: '../device/stat/faultHandleDuration',
                type: 'POST',
                params: JSON.stringify(param),
                contentType: 'application/json',
                successCallback: function (res) {
                    myChart.hideLoading();
                    self.loadWorkList('JIEDAN');
                    // 渲染工单执行时长平均统计
                    var workExecuteAvgStat = res.result;
                    var option = {
                        legend: {
                            data: workExecuteAvgStat.legendData
                        },
                        series: [{
                            data: workExecuteAvgStat.seriesData,
                        }]
                    };
                    myChart.setOption(option);
                }
            });
        },
        loadWorkList: function (key) {
            var param = {};
            if (this.q.month) {
                param.month = this.convertGMT2Month(this.q.month);
            }
            if (this.q.deptId) {
                param.deptId = this.q.deptId;
            }
            param.orderField = key;
            $("#jqGrid").jqGrid('setGridParam', {
                url: '../device/stat/faultHandleDurationTopWork',
                search: true,
                postData: param
            }).trigger("reloadGrid");
        },
        reset: function () {
            this.q = {
                month: '', // 统计月份
                dcode: '', // 设备组织CODE
                num: 10 // 最多显示设备数
            };
            this.search();
        },
        convertGMT2Month: function (time) {
            var date = new Date(time)
            var month = (date.getMonth() + 1);
            return date.getFullYear() + '-' + (month < 10 ? '0' : '') + month;
        }
    },
    mounted: function () {
        let _this = this;
        // 初始化工单执行平均时长统计报表
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option, true);
        myChart.on('click', function (params) {
            var name = params.name;
            if(name =='接单'){
                key = 'JIEDAN';
            }else if(name =='接单'){
                key = 'WEIXIU';
            }else{
                key = 'QUEREN';
            }
            _this.loadWorkList(key);
        });
        // 初始化加载数据
        this.search();
    }
});
